﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TSON.GeoCOM
{
    public enum AUT_POSMODE
    {
        AUT_NORMAL = 0, // fast positioning mode

        AUT_PRECISE = 1,// exact positioning mode
        // note: can distinctly claim more time
        // for the positioning

        AUT_Fast = 2,// for TS30 / TM30 instruments,
        //positions with the last valid inclination
        //and an increased positioning tolerance.
    }

    public enum AUT_ATRMODE // Possible modes of the target recognition
    {
        AUT_POSITION = 0, // Positioning to the hz- and v-angle

        AUT_TARGET = 1 // Positioning to a target in the
        // environment of the hz- and v-angle.
    }

    public enum AUT_ADJMODE    // Possible settings of the positioning tolerance relating the angle- 
                        //or the point- accuracy at the fine adjust.
    {
        AUT_NORM_MODE = 0, // Angle tolerance

        AUT_POINT_MODE = 1, // Point tolerance

        AUT_DEFINE_MODE = 2, // System independent positioning tolerance. Set with AUT_SetTol
    }

    public enum AUT_SRH_DIR
    {
        AUT_CLOCKWISE = 1, // direction clockwise.

        AUT_ANTICLOCKWISE = -1, // direction counter clockwise.
    }


    public class AUT
    {
        public static RC ReadTol(TsSerialPort com, ref string outString)
        {
            string responseString = com.getResponse(string.Format("\n%R1Q,9008:\r\n"));
            responseString = responseString.Trim();
            //Tolerance Hz[double],Tolerance V[double]
            outString = Helpers.getInfoString(responseString);
            return Helpers.getRcCode(responseString);
        }
        public static RC SetTol(TsSerialPort com, double toleranceHz, double toleranceV)
        {
            string responseString = com.getResponse(string.Format("\n%R1Q,9007:{0},{1}\r\n",
                toleranceHz.ToString(), toleranceV.ToString()));
            responseString = responseString.Trim();
            return Helpers.getRcCode(responseString);
        }
        public static RC ReadTimeout(TsSerialPort com, ref string outString)
        {
            string responseString = com.getResponse(string.Format("\n%R1Q,9012:\r\n"));
            responseString = responseString.Trim();
            //TimeoutHz[double], TimeoutV[double]
            outString = Helpers.getInfoString(responseString);
            return Helpers.getRcCode(responseString);
        }
        public static RC SetTimeout(TsSerialPort com, double timeoutHz, double timeoutV)
        {
            string responseString = com.getResponse(string.Format("\n%R1Q,9011:{0},{1}\r\n",
                timeoutHz.ToString(), timeoutV.ToString()));
            responseString = responseString.Trim();
            return Helpers.getRcCode(responseString);
        }
        public static RC MakePositioning(TsSerialPort com, double Hz,double V,AUT_POSMODE POSMode,AUT_ATRMODE ATRMode,bool bDummy)
        {
            string responseString = com.getResponse(string.Format("\n%R1Q,9027:{0},{1},{2},{3},{4}\r\n",
                Hz.ToString(), V.ToString(), Convert.ToInt32(POSMode).ToString(),
                Convert.ToInt32(ATRMode).ToString(), Convert.ToInt32(bDummy).ToString()));
            responseString = responseString.Trim();
            return Helpers.getRcCode(responseString);
        }
        public static RC MakePositioning(TsSerialPort com, double Hz, double V, AUT_POSMODE POSMode, AUT_ATRMODE ATRMode)
        {
            string responseString = com.getResponse(string.Format("\n%R1Q,9027:{0},{1},{2},{3},0\r\n",
                Hz.ToString(), V.ToString(), Convert.ToInt32(POSMode).ToString(),
                Convert.ToInt32(ATRMode).ToString()));
            responseString = responseString.Trim();
            return Helpers.getRcCode(responseString);
        }
        public static RC ChangeFace(TsSerialPort com, AUT_POSMODE POSMode, AUT_ATRMODE ATRMode)
        {
            string responseString = com.getResponse(string.Format("\n%R1Q,9028:{0},{1},0\r\n",
                Convert.ToInt32(POSMode).ToString(),Convert.ToInt32(ATRMode).ToString()));
            responseString = responseString.Trim();
            return Helpers.getRcCode(responseString);
        }
        public static RC ChangeFace(TsSerialPort com, AUT_POSMODE POSMode, AUT_ATRMODE ATRMode, bool bDummy)
        {
            string responseString = com.getResponse(string.Format("\n%R1Q,9028:{0},{1},{2}\r\n",
                Convert.ToInt32(POSMode).ToString(), Convert.ToInt32(ATRMode).ToString(),
                Convert.ToInt32(bDummy).ToString()));
            responseString = responseString.Trim();
            return Helpers.getRcCode(responseString);
        }
        public static RC FineAdjust(TsSerialPort com, Double dSrchHz,double dSrchV,bool bDummy)
        {
            string responseString = com.getResponse(string.Format("\n%R1Q,9037:{0},{1},{2}\r\n",
                dSrchHz.ToString(), dSrchV.ToString(), Convert.ToInt32(bDummy).ToString()));
            responseString = responseString.Trim();
            return Helpers.getRcCode(responseString);
        }
        public static RC FineAdjust(TsSerialPort com, Double dSrchHz, double dSrchV)
        {
            string responseString = com.getResponse(string.Format("\n%R1Q,9037:{0},{1},0\r\n",
                dSrchHz.ToString(), dSrchV.ToString()));
            responseString = responseString.Trim();
            return Helpers.getRcCode(responseString);
        }
        public static RC Search(TsSerialPort com, double Hz_Area, double V_Area, bool bDummy)
        {
            string responseString = com.getResponse(string.Format("\n%R1Q,9029:{0},{1},{2}\r\n",
                Hz_Area.ToString(), V_Area.ToString(), Convert.ToInt32(bDummy).ToString()));
            responseString = responseString.Trim();
            return Helpers.getRcCode(responseString);
        }
        public static RC Search(TsSerialPort com, double Hz_Area, double V_Area)
        {
            string responseString = com.getResponse(string.Format("\n%R1Q,9029:{0},{1},0\r\n",
                Hz_Area.ToString(), V_Area.ToString()));
            responseString = responseString.Trim();
            return Helpers.getRcCode(responseString);
        }

        public static RC GetFineAdjustMode(TsSerialPort com, ref string outString)
        {
            string responseString = com.getResponse(string.Format("\n%R1Q,9030:\r\n"));
            responseString = responseString.Trim();
            //AdjMode[integer]
            outString = Helpers.getInfoString(responseString);
            return Helpers.getRcCode(responseString);
        }
        public static RC SetFineAdjustMode(TsSerialPort com, AUT_ADJMODE adjMode)
        {
            string responseString = com.getResponse(string.Format("\n%R1Q,9031:{0}\r\n",
                Convert.ToInt32(adjMode).ToString()));
            responseString = responseString.Trim();
            return Helpers.getRcCode(responseString);
        }
        public static RC LockIn(TsSerialPort com)
        {
            string responseString = com.getResponse(string.Format("\n%R1Q,9013:\r\n"));
            responseString = responseString.Trim();
            return Helpers.getRcCode(responseString);
        }
        public static RC GetSearchArea(TsSerialPort com, ref string outString)
        {
            string responseString = com.getResponse(string.Format("\n%R1Q,9042:\r\n"));
            responseString = responseString.Trim();
            //dCenterHz [double], dCenterV [double], dRangeHz [double], dRangeV [double], bEnabled [Boolean]
            outString = Helpers.getInfoString(responseString);
            return Helpers.getRcCode(responseString);
        }
        public static RC SetSearchArea(TsSerialPort com, double dCenterHz,double dCenterV,double dRangeHz,double dRangeV,bool bEnabled)
        {
            string responseString = com.getResponse(string.Format("\n%R1Q,9043:{0},{1},{2},{3},{4}\r\n",
                dCenterHz.ToString(), dCenterV.ToString(), dRangeHz.ToString(), dRangeV.ToString(),
                Convert.ToInt32(bEnabled).ToString()));
            responseString = responseString.Trim();
            return Helpers.getRcCode(responseString);
        }
        public static RC GetUserSpiral(TsSerialPort com, ref string outString)
        {
            string responseString = com.getResponse(string.Format("\n%R1Q,9040:\r\n"));
            responseString = responseString.Trim();
            //dRangeHz[double],dRangeV[double]
            outString = Helpers.getInfoString(responseString);
            return Helpers.getRcCode(responseString);
        }
        public static RC SetUserSpiral(TsSerialPort com, double dRangeHz,double dRangeV)
        {
            string responseString = com.getResponse(string.Format("\n%R1Q,9041:{0},{1}\r\n",
                dRangeHz.ToString(), dRangeV.ToString()));
            responseString = responseString.Trim();
            return Helpers.getRcCode(responseString);
        }
        public static RC PS_EnableRange(TsSerialPort com, bool bEnable)
        {
            string responseString = com.getResponse(string.Format("\n%R1Q,9048:{0}\r\n",
                Convert.ToInt32(bEnable).ToString()));
            responseString = responseString.Trim();
            return Helpers.getRcCode(responseString);
        }
        public static RC PS_SetRange(TsSerialPort com, int lMinDist, int lMaxDist)
        {
            string responseString = com.getResponse(string.Format("\n%R1Q,9047:{0},{1}\r\n",
                lMinDist.ToString(), lMaxDist.ToString()));
            responseString = responseString.Trim();
            return Helpers.getRcCode(responseString);
        }
        public static RC PS_SearchWindow(TsSerialPort com)
        {
            string responseString = com.getResponse(string.Format("\n%R1Q,9052:\r\n"));
            responseString = responseString.Trim();
            return Helpers.getRcCode(responseString);
        }
        public static RC PS_SearchNext(TsSerialPort com, AUT_SRH_DIR lDirection, bool bSwing)
        {
            string responseString = com.getResponse(string.Format("\n%R1Q,9051:{0},{1}\r\n",
                Convert.ToInt32(lDirection).ToString(), Convert.ToInt32(bSwing).ToString()));
            responseString = responseString.Trim();
            return Helpers.getRcCode(responseString);
        }
        public static RC CAM_PositToPixelCoord(TsSerialPort com, CAM_ID_TYPE camID, double dXCoord, double dYCoord)
        {
            string responseString = com.getResponse(string.Format("\n%R1Q,9081:{0},{1},{2}\r\n",
                Convert.ToInt32(camID).ToString(), dXCoord.ToString(), dYCoord.ToString()));
            responseString = responseString.Trim();
            return Helpers.getRcCode(responseString);
        }
    }    
}
